package org.kfive.nova.hadoop.mapper;


import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 16-3-28
 * Time: 下午10:17
 * To change this template use File | Settings | File Templates.
 */
public class MaxTemperatureMapper extends Mapper<LongWritable,Text,Text,IntWritable>{

    private static final int MISSING = 9999;

    public void map(LongWritable key,Text value,Context context) throws IOException,InterruptedException{

        Counter availableTempCounter = context.getCounter("nova","availableTemp");
        String line = value.toString();

        String year = line.substring(15,19);

        int airTemperature;

        if(line.charAt(87)=='+'){

            airTemperature = Integer.parseInt(line.substring(88,92));

        }else{

            airTemperature = Integer.parseInt(line.substring(87,92));
        }

        String quality = line.substring(92,93);

        if(airTemperature!=MISSING && quality.matches("[01459]")){
            availableTempCounter.increment(1);
            context.write(new Text(year),new IntWritable(airTemperature));
        }
    }

}
